package com.hulk.ratel.test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import fi.iki.elonen.NanoHTTPD;
import lombok.extern.slf4j.Slf4j;

@Slf4j
    public class App extends NanoHTTPD {
    
        public App() throws IOException {
            super(8080);
            start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
            System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
        }
    
        public static void main(String[] args) {
            try {
                new App();
            } catch (IOException ioe) {
                System.err.println("Couldn't start server:\n" + ioe);
            }
        }
    
        @Override
        public Response serve(IHTTPSession session) {
            Method method = session.getMethod();
            log.info("weifang","Method:"+method.toString());

            if(NanoHTTPD.Method.GET.equals(method)){
                //get方式
                String queryParams = session.getQueryParameterString();
                 log.info("weifang","params:"+queryParams);
            }
//            else if(NanoHTTPD.Method.POST.equals(method)){
//                //post方式
//                 log.info("weifang","into POST");
//                try {
//                    session.parseBody(new HashMap<String, String>());
//                    String body = session.getQueryParameterString();
//
//                    JSONObject json = new JSONObject(body);
//
//                    String a = json.getString("a");
//                    String b = json.getString("b");
//                    String c = json.getString("c");
//                    String d = json.getString("d");
//                     log.info("weifang","json:"+ body);
//                     log.info("weifang","a:"+ a + " b:" + b + " c:" + c + " d:" + d);
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                } catch (ResponseException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                } catch (JSONException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
            else if(NanoHTTPD.Method.POST.equals(method)){
                Map<String, String> files = new HashMap<String, String>();
    /*获取header信息，NanoHttp的header不仅仅是HTTP的header，还包括其他信息。*/
                Map<String, String> header = session.getHeaders();

                try {
                    session.parseBody(files);
                    String param=files.get("postData");

                    log.info("header : " + header);
                    log.info("files : " + files);
                    log.info("param : " + param);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ResponseException e) {
                    e.printStackTrace();
                }
            }else {
                log.error("测试方法不支持");
            }
            return super.serve(session);
        }
    }
